package com.example.toutiao.ui.main.frg


import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import cn.jzvd.Jzvd
import com.example.toutiao.R
import com.example.toutiao.base.BaseFragment
import com.example.toutiao.ui.main.act.SearchActivity
import com.example.toutiao.ui.main.adpater.VideoTabAdapter
import com.example.toutiao.ui.main.model.RecommenModel
import com.example.toutiao.ui.main.model.TabIdsModel
import com.example.toutiao.ui.main.model.TabListModel
import com.example.toutiao.ui.main.presenter.HomePresenter
import com.example.toutiao.ui.main.view.HomeView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.view.ChannelDialog
import kotlinx.android.synthetic.main.fragment_home.view.*

class HomeFragment : BaseFragment<HomeView, HomePresenter>(), HomeView {
    private var titles: MutableList<TabIdsModel>? = mutableListOf<TabIdsModel>()
    private var fragments: MutableList<Fragment> = mutableListOf<Fragment>()
    private var videoAdapter: VideoTabAdapter? = null
    private var channelList: TabListModel? = null
    private var views: View? = null
    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initView(view: View) {
        views = view
        view.rec_title.setOnClickListener {
            startActivity(SearchActivity::class.java)
        }
        view.iv_channel.setOnClickListener {
            val dialog=ChannelDialog(channelList!!)
            dialog.show(childFragmentManager,"dialog")
        }
    }

    override fun initData() {
        getPresenter()!!.getTabIds()
        getPresenter()!!.getRecommend()
        getPresenter()!!.getChannel()
    }

    override fun createPresenter(): HomePresenter = HomePresenter()

    override fun isRegister(): Boolean = true
    override fun setRecommend(data: MutableList<RecommenModel>) {
        if(data!=null){
            val sb=StringBuffer()
            for(i:Int in data.indices){
                if(i<data.size-1){
                    sb.append("${data[i].name}  |  ")
                }else{
                    sb.append("${data[i].name}")
                }
            }
            views!!.rec_title.text="$sb"
            postMsg(EvtMsgModel(401, sb))
        }
    }

    override fun setChannel(data: TabListModel) {
        if(data!=null){
            channelList=data
        }
    }

    override fun <T> setData(data: T) {
        titles = data as MutableList<TabIdsModel>
        for (i: Int in titles!!.indices) {
            fragments.add(HomeListFragment(titles!![i].id))
        }
        if (videoAdapter == null) {
            videoAdapter = VideoTabAdapter(childFragmentManager, fragments, titles!!)
        } else {
            videoAdapter!!.notifyDataSetChanged()
        }
        views!!.vp_video.offscreenPageLimit = fragments.size
        views!!.vp_video.adapter = videoAdapter
        /* 监听页面变化 使用最新的 */
        views!!.stl_tab_title.setViewPager(views!!.vp_video)
    }

    override fun setError(err: String) {

    }

}